#include "xqat_json.h"
#include "QDir"
#include "QtCore"
#include "QTextStream"
xqat_json::xqat_json()
{
}

void  xqat_json::addjsondata(QString Path)
{
    /* Configuration */
    QJsonObject pageObject1;
    pageObject1.insert("name", QString("default"));
    pageObject1.insert("excel-path", QString("default"));
    pageObject1.insert("gf-path", QString("default"));
    /* theme */
    QJsonObject pageObject2;
    pageObject2.insert("font size", QString("10"));
    pageObject2.insert("font", QString("default"));
    pageObject2.insert("font color", QString("default"));
    pageObject2.insert("backgroundImage", QString("default"));
    pageObject2.insert("backgroundImageOpacity", QString("default"));
    /* Create json object */
    QJsonObject json;
    json.insert("Configuration", QJsonValue(pageObject1));
    json.insert("theme", QJsonValue(pageObject2));
    /* Create write json object */
    QJsonDocument document;
    document.setObject(json);
    QByteArray byte_array = document.toJson(QJsonDocument::Indented);
    QString json_str(byte_array);       /* QByteArray to QString */
    /* Write data */
    QFile file(Path);
    file.open(QIODevice::WriteOnly);    /* Open file */
    QTextStream stream(&file);
    stream << (json_str);               /* Write data */
    file.close();
}


bool  xqat_json::parsejsondata(QString Path, struct initdata_json_t *jsondata)
{
    QFile file(Path);
    file.open(QIODevice::ReadOnly);                 /* Open file */
    QTextStream stream(&file);                      /* Read data */
    QString byteArray;
    byteArray = stream.readAll();
    file.close();
    QJsonParseError jsonError;
    QJsonDocument document = QJsonDocument::fromJson(byteArray.toLatin1(), &jsonError);
    if(!document.isNull() && (jsonError.error == QJsonParseError::NoError)) {
        if(document.isObject()) {
            QJsonObject object = document.object();
            if(object.contains("Configuration")) {
                QJsonValue value = object.value("Configuration");
                if(value.isObject()) {
                    QJsonObject obj = value.toObject();
                    if (obj.contains("name")) {
                        QJsonValue value = obj.value("name");
                        if (value.isString()) {
                            QString strHome = value.toString();
                            jsondata->name = strHome;                   /* Copy  jsondata->name json data*/
                            qDebug() << "name : " << strHome;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    if (obj.contains("gf-path")) {
                        QJsonValue value = obj.value("gf-path");
                        if (value.isString()) {
                            QString strHome = value.toString();
                            jsondata->gf_path = strHome;                /* Copy  jsondata->gf_path json data*/
                            qDebug() << "gf-path : " << strHome;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    if (obj.contains("excel-path")) {
                        QJsonValue value = obj.value("excel-path");
                        if (value.isString()) {
                            QString strHome = value.toString();
                            jsondata->excel_path = strHome;           /* Copy  jsondata->excel_path json data*/
                            qDebug() << "excel-path : " << strHome;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
            if(object.contains("theme")) {
                QJsonValue value = object.value("theme");
                if(value.isObject()) {
                    QJsonObject obj = value.toObject();
                    if (obj.contains("backgroundImage")) {
                        QJsonValue value = obj.value("backgroundImage");
                        if (value.isString()) {
                            QString strHome = value.toString();
                            jsondata->backgroundImage = strHome; /* Copy  jsondata->backgroundImage json data*/
                            qDebug() << "backgroundImage : " << strHome;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    if (obj.contains("backgroundImageOpacity")) {
                        QJsonValue value = obj.value("backgroundImageOpacity");
                        if (value.isString()) {
                            QString strHome = value.toString();
                            jsondata->backgroundImageOpacity = strHome;  /* Copy  jsondata->backgroundImageOpacity json data*/
                            qDebug() << "backgroundImageOpacity : " << strHome;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    if (obj.contains("font")) {
                        QJsonValue value = obj.value("font");
                        if (value.isString()) {
                            QString strHome = value.toString();
                            jsondata->font = strHome;       /* Copy  jsondata->font json data*/
                            qDebug() << "font : " << strHome;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    if (obj.contains("font color")) {
                        QJsonValue value = obj.value("font color");
                        if (value.isString()) {
                            QString strHome = value.toString();
                            jsondata->font_color = strHome; /* Copy  jsondata->font_color json data*/
                            qDebug() << "font color : " << strHome;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    if (obj.contains("font size")) {
                        QJsonValue value = obj.value("font size");
                        if (value.isString()) {
                            QString strHome = value.toString();
                            jsondata->font_size = strHome;  /* Copy  jsondata->font_size json data*/
                            qDebug() << "font size : " << strHome;
                        } else {
                            return false;
                        }
                    } else {
                        return false;
                    }
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}


//bool  xqat_json::Create_Folder_jsondata(struct file_path_file_t *path)
//{
//    QString tmp = QDir::currentPath();              /* Get the program run path */
//    path->master_path = tmp;
//    tmp.append(QString("/.settings"));              /* Add Folder path,it waiting to be create */
//    path->setting_path = tmp;
//    QDir dir(path->setting_path);                   /* Create DIR object */
//    tmp.append(QString("/settings.json"));
//    path->setting_file_name = tmp;                  /* Copy  path->setting_file_name data*/
//    QFile file(path->setting_file_name);
//    if(dir.exists()) {                              /* If the path exists */
//        if(file.exists() == false) {
//            /* Create setting json_file */
//            file.open(QIODevice::WriteOnly);
//            file.close();
//            addjsondata(path->setting_file_name);   /* Add json data */
//        }
//    } else {                                        /* If the path does not exist */
//        dir.mkdir(path->setting_path);              /* Create Folder */
//        /* Create setting json_file */
//        file.open(QIODevice::WriteOnly);
//        file.close();
//        addjsondata(path->setting_file_name);       /* Add json data */
//    }
//    file.open(QIODevice::ReadOnly);
//    if(file.exists() == true) {
//        QTextStream stream(&file);                      /* Read data */
//        QString tmp;
//        stream >> tmp;
//        /* If the data is not NULL, another meaning is first data is "{" */
//        if(tmp.indexOf(QString("{")) >= 0) {
//            QString tmp;
//            return true;
//        } else {
//            return false;
//        }
//    } else {
//        return  false;
//    }
//}

